package pl.edu.romanow.snippet


import net.liftweb.util.Helpers._
import pl.edu.romanow.util.HTMLUtil
import pl.edu.romanow.model.{GraphVisualisationUtil, Database, Facebook}
import scala.collection.JavaConversions._
import pl.edu.romanow.alg.ClusteringActor
import net.liftweb.http.js.jquery.JqJsCmds
import xml.{Text, NodeSeq}
import net.liftweb.common.Full
import net.liftweb.http.{IdMemoizeTransform, SHtml, DispatchSnippet}
import pl.edu.romanow.query.{QueryItem, ConnectionItem, Query, Queries}
import net.liftweb.http.js.{JsCmds, JsCmd}

/**
 * Created by IntelliJ IDEA.
 * User: jar
 * Date: 29.04.12
 * Time: 17:02
 * To change this template use File | Settings | File Templates.
 */

class LeftMenu extends DispatchSnippet {

  override def dispatch = {
    case "menu" if !Facebook.currentUser.is.isEmpty => th => menu(th)
    case _ => el => NodeSeq.Empty
  }


  def menu(in: NodeSeq, query: Query = Query("me", Nil)): NodeSeq = {

    var currentId = Facebook.currentUser.openTheBox.id

    var newQurey = (System.nanoTime().toString, new ConnectionItem(Full("frieds"), true))

    var queries = List[(String, QueryItem)]()

    def showQuery(q: Query): JsCmd = {

      q.drawQuery match {
        case (data, true) => GraphSnipped.reDrawGraph(data) &
          JqJsCmds.DisplayMessage("graph_messages", Text("Your graph is to big. It wos reduced."), 5.seconds, 2.seconds)
        case (data, _) => GraphSnipped.reDrawGraph(data)
      }
    }


    def add_to_end(outer: IdMemoizeTransform) =
      HTMLUtil.ajaxJsFunction("add_to_end", "id" :: Nil, id => HTMLUtil.ajaxCall {
        queries = newQurey :: queries
        query.items = newQurey._2 :: query.items
        newQurey = (System.nanoTime().toString, new ConnectionItem(Full("frieds"), true))
        outer.setHtml() & JsCmds.Run("makeDnD();")
      })

    def removeFromQuery(outer: IdMemoizeTransform) =
      HTMLUtil.ajaxJsFunction("remove_from_query", "id" :: Nil, id => HTMLUtil.ajaxCall {
        queries.filter(_._1 == id.head) match {
          case h :: Nil =>
            query.items = query.items.filter(_ != h)
            queries = queries.filter(_._1 != id.head)
            outer.setHtml() & JsCmds.Run("makeDnD();")
          case _ => outer.setHtml() & JsCmds.Run("makeDnD();")
        }
      })

    def setBeg(outer: IdMemoizeTransform) = HTMLUtil.ajaxJsFunction("setBeg", "id" :: Nil, id => HTMLUtil.ajaxCall({
      query.startId = id.head;
      outer.setHtml() & JsCmds.Run("makeDnD();");
    }))

    def move_to_head(outer: IdMemoizeTransform) =
      HTMLUtil.ajaxJsFunction("move_to_head", "id" :: Nil, id => HTMLUtil.ajaxCall {
        JsCmds.Noop
      })

    ("#left_menu_content" #> SHtml.idMemoize {
      outer =>
        "#item" #> newQurey._2.bind(false) &
          "#item [id]" #> newQurey._1 &
          "#do_premake" #> Queries.all.map {
            el =>
              "#do_premake *" #> el._1 &
                "#do_premake [onclick]" #> HTMLUtil.ajaxCall {
                  showQuery(el._2(currentId))
                }
          } &
          "#start_crawl [onclick]" #> HTMLUtil.ajaxCall(Facebook.fullCrawl()) &
          "#start_cluseter [onclick]" #> HTMLUtil.ajaxCall(ClusteringActor.doCluster(query)) &
          "#sel_item" #> queries.reverse.map {
            el =>
              "#sel_item *" #> el._2.bind(false) &
                "#sel_item [id]" #> el._1
          } &
          "#start_query" #> SHtml.ajaxText(query.startId, currentId = _) &
          "#drag_success" #> JsCmds.Script(add_to_end(outer) & removeFromQuery(outer)
            & move_to_head(outer) & setBeg(outer)) &
          "#showQuery [onclick]" #> HTMLUtil.ajaxCall {
            showQuery(query)
          }

    })(in)
  }


}


